package com.woniu.houtai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.houtai.entity.Address;
import com.woniu.houtai.entity.Order;
import com.woniu.houtai.entity.Orderitem;
import com.woniu.houtai.entity.Product;
import com.woniu.houtai.mapper.*;
import com.woniu.houtai.service.OrderService;
import com.woniu.houtai.vo.OrderVo;
import com.woniu.houtai.vo.OrderitemVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author firstGroup
 * @since 2023-02-22 09:45:58
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    RabbitTemplate rabbitTemplate;

    //注入模板
    @Autowired
    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }


    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ShopMapper shopMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    OrderitemMapper orderitemMapper;
    @Autowired
    ProductMapper productMapper;

    @Override
    public void updateOrderState(Integer orderId, Integer state) {
        //通过订单id获取订单信息
        Order order = orderMapper.selectById(orderId);
        //产看订单状态
        Integer status = order.getStatus();
        System.out.println("未修改前的订单状态:" + status);
        //更新订单状态
        order.setStatus(state);
        int updateById = orderMapper.updateById(order);
        System.out.println("更新订单状态updateById:" + updateById);
    }

    //张朝朝：通过商家id获取当前商家的所有订单信息
    @Override
    public List<OrderVo> selectOrderVo(Integer shopId, Integer status) {
        System.out.println(status);
        //通过商家Id获取到订单信息
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        //判断查询的状态
        if (status == null) {
                    orderQueryWrapper.eq("shop_id", shopId);
        } else {
            orderQueryWrapper.eq("shop_id", shopId);
            orderQueryWrapper.eq("status", status);
        }
        List<Order> orderList = orderMapper.selectList(orderQueryWrapper.orderByDesc("id"));
        System.out.println(79+"size-------------------"+orderList.size());
        //创建一个集合,储存所有订单和订单的所有详细订单
        List<OrderVo> orderVoList = new ArrayList<>();

        for (Order order : orderList) {
            OrderVo orderVo = new OrderVo();
            orderVo.setId(order.getId());   //订单编号
            orderVo.setCreateTime(order.getCreateTime());//下单时间
            orderVo.setSumPrice(order.getSumPrice());//下单金额
            orderVo.setStatus(order.getStatus());//订单状态

            Address address = addressMapper.selectById(order.getAddressId());
            orderVo.setAddressName(address.getAddress());//收货地址
            orderVo.setReceiveName(address.getReceiveName());//收货人
            orderVo.setTel(address.getTel());//收货人电话


            //根据orderId 查询出对应的订单详情
            QueryWrapper<Orderitem> orderitemQueryWrapper = new QueryWrapper<>();
            orderitemQueryWrapper.eq("order_id", order.getId()).and(new Consumer<QueryWrapper<Orderitem>>() {

                @Override
                public void accept(QueryWrapper<Orderitem> orderitemQueryWrapper) {
                    orderitemQueryWrapper.eq("user_id", order.getUserId());
                }
            });
            List<Orderitem> orderitems = orderitemMapper.selectList(orderitemQueryWrapper);
            //将Orderitem集合封装成OrderItemVo集合
            List<OrderitemVo> orderitemVoList = new ArrayList<>();
            for (Orderitem orderitem : orderitems) {
                //将Orderitem对象封装成orderitemVo对象
                OrderitemVo orderitemVo = new OrderitemVo();
                Product product = productMapper.selectById(orderitem.getProductId());
                orderitemVo.setName(product.getName());//商品名字
                orderitemVo.setPrice(product.getPrice());//商品单价
                orderitemVo.setCount(orderitem.getCount()); //商品数量
                //将OrderItemVo对象加入orderitemVoList集合
                orderitemVoList.add(orderitemVo);
            }
            orderVo.setOrderItemVoList(orderitemVoList);
            //将orderVo对象加入orderVoList集合
            orderVoList.add(orderVo);
        }
        return orderVoList;
    }

    //开启自动接单,修改商家所有为3的订单
    @Override
    public void updateOrders(Integer shopId) {
        //修改用户的订单
        UpdateWrapper<Order> updateWrapper= new UpdateWrapper<>();
        updateWrapper.set("status",4).in("shop_id",shopId);
        updateWrapper.eq("status",3);
        int update = orderMapper.update(null, updateWrapper);
        System.out.println("修改了："+update);


    }

}
